package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.VO.BrandListVO;
import cn.tedu.csmall.product.pojo.VO.BrandStandardVO;
import cn.tedu.csmall.product.pojo.VO.CategoryListVO;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.repo.IBrandRedisRepository;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private IBrandRedisRepository repository;
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private SpuMapper spuMapper;

    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO) {
        log.debug("开始处理添加品牌业务，参数{}",brandAddNewDTO);
        String name = brandAddNewDTO.getName();
        int countByName = brandMapper.countByName(name);
        if(countByName>0){
            String message = "添加失败，名称被占用!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO,brand);
        log.debug("准备插入品牌数据，参数{}",brand);
        int rows = brandMapper.insert(brand);
        if(rows!=1){
            String message = "添加品牌失败，服务器繁忙，请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除品牌】的业务，接收到的参数是：{}",id);
        BrandStandardVO standardById = brandMapper.getStandardById(id);
        if(standardById == null){
            String message = "删除失败，该数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i = brandCategoryMapper.countByBrand(id);
        if(i>0){
            String message = "删除失败，该数据与类别品牌表关联";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int i1 = spuMapper.countByBrand(id);
        if(i1>0){
            String message = "删除失败，该数据与SPU表关联";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        log.debug("准备执行删除！！！");
        int i2 = brandMapper.deleteById(id);
        if(i2 != 1){
            String message = "删除错误，当前服务器繁忙，请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void update(Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【修改品牌】业务，接收到的参数是：{}",brandUpdateDTO);
        BrandStandardVO standardById = brandMapper.getStandardById(id);
        if(standardById == null){
            String message = "修改失败，该数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i = brandMapper.countByNameAndNotId(brandUpdateDTO.getName(), id);
        if(i>0){
            String message = "修改失败，该数据名称已被占用";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandUpdateDTO,brand);
        brand.setId(id);
        int update = brandMapper.update(brand);
        if(update != 1){
            String message = "修改错误，当前服务器繁忙，请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("开始处理【查询详情】的业务，接收到的参数是：{}",id);
        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        if (brandStandardVO == null) {
            String message = "查询失败，该数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return brandStandardVO;
    }

    @Override
    public List<BrandListVO> getList() {
        log.debug("开始处理【查询列表】的业务，无参数");
//        List<BrandListVO> list = brandMapper.list();
        List<BrandListVO> list = repository.getAll();
        for(Object lists: list){
            log.debug("列表详情：{}",lists);
        }
        return list;
    }



    @Override
    public void rebuildCache() {
        /**
         * 删除缓存数据
         */
        repository.deleteAll();
        /**
         * 写入数据列表
         */
        List<BrandListVO> list = brandMapper.list();
        repository.save(list);
        /**
         * 写入数据详情
         */
        for (BrandListVO listVO : list) {
            Long brandId = listVO.getId();
            BrandStandardVO standardVO = brandMapper.getStandardById(brandId);
            repository.save(standardVO);
        }
    }
}
